/**
 * Created with JetBrains PhpStorm.
 * User: shaoshuai
 * Date: 12-7-19
 */
FlyFight.controller.User = Zen.extend(Zen.Controller, {
    isLogin: false,
    initController: function () {
        console.log('User controller init');
        this.control('#login-btn', 'click', 'login');
    },
    login: function (target, event) {
        event.preventDefault();
        var that = this;
        var username = jQuery('#login-form input').val();
        if (username != '') {
            jQuery.ajax({
                url: 'server/login.php',
                data: {
                    username: username
                },
                success: function (data) {
                    that.isLogin = true;
                    jQuery('#login-div input').hide();
                    jQuery('#login-div span').html(username).show();
                    jQuery('#login-button').hide();
                    jQuery('#logout-button').show();
                    var result = jQuery.parseJSON(data);
                    var list = [];
                    if (result['success']) {
                        list = result['data'];
                        /**
                         * @type {FlyFight.view.UserList}
                         */
                        var userList = Zen.View.singleton(FlyFight.view.UserList);
                        userList.update(list);
                        comet();
                    } else {
                        console.log('login error');
                    }
                }
            });
        }
    }
});
